/*
 * @Author: your name
 * @Date: 2021-10-28 22:18:44
 * @LastEditTime: 2021-12-19 23:09:08
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \phone\src\select_itemv2.cpp
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "select_itemv2.h"
SelectItemV2::SelectItemV2()
{
    selectData = 0;
    funcAmount = 0;
    setEvent[static_cast<int>(KeyboardValue::KEY_DIRECTION_LEFT)] = {std::bind(&SelectItemV2::SelectFunc, this, _1)};
    setEvent[static_cast<int>(KeyboardValue::KEY_DIRECTION_RIGHT)] = {std::bind(&SelectItemV2::SelectFunc, this, _1)};
    setEvent[static_cast<int>(KeyboardValue::KEY_SOFT_KEY_3)] = {std::bind(&SelectItemV2::SelectFunc, this, nullptr)};
    setEvent[static_cast<int>(KeyboardValue::KEY_SOFT_KEY_4)] = {std::bind(&SelectItemV2::SaveData, this, _1)};
}
SelectItemV2::~SelectItemV2()
{
    Widget *contrl = GetContrlView();
    if (contrl && !contrl->IsValid())
    {
        LOG_E("contrl view is valid.\n");
        return;
    }
    std::unique_ptr<Widget> view(GuiEngine::GetInstance().NewWidget());
    if (GuiEngine::GetInstance().Lookup(contrl, "valueString", true, view.get()))
    {
        GuiEngine::GetInstance().SetSelfLayout(view.get(), "default(w=52%)");
    }
    if (GuiEngine::GetInstance().Lookup(contrl, "selectIcon", true, view.get()))
    {
        GuiEngine::GetInstance().Destroy(view.get());
    }
}
void SelectItemV2::RefreshPage()
{
    RefreshPageCommon(GetString());
    /**
     * @brief
     * 重置一次设置项聚焦属性，否则字符串变化超长时不会滚动。
     */
    GuiEngine::GetInstance().SetFocus(GetContrlView(), false);
    GuiEngine::GetInstance().SetFocus(GetContrlView(), true);
}
void SelectItemV2::ResetKeyCmd(KeyCmdMapTable &keyEvent)
{
    keyEvent.table[static_cast<int>(KeyboardValue::KEY_DIRECTION_LEFT)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    keyEvent.table[static_cast<int>(KeyboardValue::KEY_DIRECTION_RIGHT)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    keyEvent.table[static_cast<int>(KeyboardValue::KEY_SOFT_KEY_3)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    keyEvent.table[static_cast<int>(KeyboardValue::KEY_SOFT_KEY_4)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    keyEvent.table[static_cast<int>(KeyboardValue::KEY_OK)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    PageHandle *window = (PageHandle *)Interface::GetInstance().GetCurrentWin();
    window->SetKeyEvent(keyEvent);
}
std::string SelectItemV2::GetString()
{
    char *string = nullptr;
    char stmp[32];
    std::string trText33 = "";
    memset(stmp, 0, 32);
    // TODO:当译文未在strings.xml列表中是，sting返回值？nullptr还是长度为0的字符串？
    GuiEngine::GetInstance().LocaleInfoTr(GetTrTextFormSettings(selectData).c_str(), &string);
    if (!string)
    {
        LOG_E("can't find any tr text\n");
        return "nullptr";
    }
    LOG_E("stringstring=%s\n", string);
    strcpy(stmp,string);
    trText33 = std::string(stmp);
    if (trText33.length() == 0)
    {
        return GetTrTextFormSettings(selectData);
    }
    return trText33;
}
void SelectItemV2::ItemSelected()
{
    Widget *contrl = GetContrlView();
    if (contrl && !contrl->IsValid())
    {
        LOG_E("contrl view is valid.\n");
        return;
    }
    /**
     * @brief
     * 改变原来的布局。
     */
    std::unique_ptr<Widget> view6(GuiEngine::GetInstance().NewWidget());
    if (GuiEngine::GetInstance().Lookup(contrl, "valueString", true, view6.get()))
    {
        GuiEngine::GetInstance().SetSelfLayout(view6.get(), "default(w=37%)");
        if (GuiEngine::GetInstance().ViewCreate(contrl, 0, 0, 0, 0, view6.get()))
        {
            /**
             * @brief
             * 新增一个左右箭头指示。
             */
            GuiEngine::GetInstance().SetSelfLayout(view6.get(), "default(w=15%)");
            GuiEngine::GetInstance().SetName(view6.get(), "selectIcon");
            GuiEngine::GetInstance().SetChildrenLayout(view6.get(), "default(r=1,c=0,align_h=center,x=0,y=0,s=0)");
            std::unique_ptr<Widget> view2(GuiEngine::GetInstance().NewWidget());
            std::unique_ptr<Widget> view3(GuiEngine::GetInstance().NewWidget());
            if (GuiEngine::GetInstance().LabelCreate(view6.get(), 0, 0, 0, 0, view3.get()))
            {
                GuiEngine::GetInstance().UseStyle(view3.get(), "itemLeft");
                GuiEngine::GetInstance().SetSelfLayout(view3.get(), "default(w=15,h=100%)");
            }
            if (GuiEngine::GetInstance().LabelCreate(view6.get(), 0, 0, 0, 0, view3.get()))
            {
                GuiEngine::GetInstance().SetSelfLayout(view3.get(), "default(w=10,h=100%)");
            }
            if (GuiEngine::GetInstance().LabelCreate(view6.get(), 0, 0, 0, 0, view3.get()))
            {
                GuiEngine::GetInstance().UseStyle(view3.get(), "itemRight");
                GuiEngine::GetInstance().SetSelfLayout(view3.get(), "default(w=15,h=100%)");
            }
        }
    }
}
void SelectItemV2::SelectFunc(void *ctx)
{
    if (ctx)
    {
        CommonFunc::GetInstance().FunctionSelection(selectData, funcAmount, ctx);
    }
    else
    {
        CommonFunc::GetInstance().FunctionSelection(selectData, funcAmount, KeyboardValue::KEY_DIRECTION_RIGHT);
    }
    RefreshPageEvent();
}
void SelectItemV2::SaveData(void *ctx)
{
    SecondPage *window = dynamic_cast<SecondPage *>(Interface::GetInstance().GetCurrentWin());
    if (window)
    {
        window->SaveConfiguration();
    }
    else
    {
        LOG_E("current window can't save anything.\n");
    }
}